package com.kuang.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Author: Zhang Dongwei
 * Date: 2022/3/16 19:02
 */
public class Demo02 {
    public static void main(String[] args) {
        Phone2 phone2  = new Phone2();
        new Thread(()->{
            phone2.sms();
        }).start();
        new Thread(()->{
            phone2.sms();
        }).start();
    }
}
class Phone2 {
    Lock lock = new ReentrantLock();

    public void sms() {
        lock.lock();//细节：lock这个两把锁，两个钥匙, synchronized一把锁
//        lock 锁必须配对，否则就是死锁在里面
        try {
            System.out.println(Thread.currentThread().getName() + "=>sms");
            call();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void call() {
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + "=>call");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}